package br.utfpr.edu.managedBean;

import java.io.IOException;
import java.util.Locale;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import br.utfpr.edu.business.NegocioException;
import br.utfpr.edu.dao.AuthenticationDao;
import br.utfpr.edu.dao.FamiliaDao;
import br.utfpr.edu.interfaces.IAutentication;

@ManagedBean(name = "authenticationFrm")
@SessionScoped
public class AuthenticationFrm extends ListaDetalhe<IAutentication>{
	
	private String login;
	private String password;
	private static final long serialVersionUID = 8468979540144744669L;
	private Locale locale;
    
	public AuthenticationFrm(){
		locale = new Locale("pt","BR");
	}
	
	public void log(ActionEvent event){
		try {
			if(login.equals("") && password.equals("")){
				imprimirErrosPagina("Informe - Login");
				imprimirErrosPagina("Informe - Senha");
			}else if(login.equals("")){
				imprimirErrosPagina("Informe - Login");
			}else if(password.equals("")){
				imprimirErrosPagina("Informe - Senha");
			}else{
				new AuthenticationDao().autenticar(login, password);
				FamiliaDao f = new FamiliaDao();
				f.pesquisar();
				FacesContext.getCurrentInstance().getExternalContext().redirect("../menu/menu.faces");
			}
		} catch (IOException e) {
			imprimirErrosPagina("Erro"+e);
		}catch (NegocioException e) {
			imprimirErrosPagina(e);
		}
	}
	
	public void setLocalePtBr(){
		locale = new Locale("pt","BR");
	}
	
	public void setLocaleEnUS(){
		locale = new Locale("en","US");
	}
	
	public void setLocaleEsp(){
		locale = new Locale("es");
	}

	@Override
	public void sort(String column, boolean ascending) {
		// TODO Auto-generated method stub
		
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Locale getLocale() {
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}
}
